package com.own.business.push.core.config;

import com.own.business.push.core.base.BasePushClientMethod;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 * PushCoreConfig
 *
 * @author chenxueli
 * @date 2023-11-16 13:33:00
 */
@Slf4j
@Component
public class PushCoreConfig {

    /**
     * 推送客户端方法
     */
    private final Map<String, BasePushClientMethod> MAP = new HashMap<>();
    @Resource
    private List<BasePushClientMethod> clientMethodList;

    @PostConstruct
    private void init() {
        log.info("初始化推送客户端");
        clientMethodList.forEach(clientMethod -> {
            var name = clientMethod.name();
            if (StringUtils.isNotBlank(name)) {
                MAP.put(name, clientMethod);
            }
        });
    }

    /**
     * 根据名称获取推送客户端方法
     *
     * @param name 名称
     * @return 推送客户端方法
     */
    public BasePushClientMethod get(String name) {
        return MAP.get(name);
    }

    /**
     * 获取所有的名称
     *
     * @return 名称列表
     */
    public Set<String> names() {
        return MAP.keySet();
    }

}
